package com.lv.leyou.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lv.leyou.common.utils.JsonUtils;
import com.lv.leyou.dto.SpecParamDTO;
import com.lv.leyou.entity.Spu;
import com.lv.leyou.entity.SpuDetail;
import com.lv.leyou.mapper.SpuDetailMapper;
import com.lv.leyou.mapper.SpuMapper;
import com.lv.leyou.service.SpecParamService;
import com.lv.leyou.service.SpuDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SpuDetailServiceImpl extends ServiceImpl<SpuDetailMapper, SpuDetail> implements SpuDetailService {
    @Autowired
    private SpuMapper spuMapper;
    @Autowired
    private SpecParamService specParamService;
    @Override
    public List<SpecParamDTO> querySpecParams(Long id, Boolean searching) {
        //根据id查询spuDetail
        SpuDetail detail = getById(id);
        //获取其中的规格参数键值对，转换为一个map
        Map<Long, Object> specValues = JsonUtils.toMap(detail.getSpecification(), Long.class, Object.class);
        //根据id查询spu，获取商品分类信息
        Spu spu = spuMapper.selectById(id);
        //根据分类id查询规格参数集合，如果seraching部位空，还要加上searching条件
        List<SpecParamDTO> params = specParamService.querySpecParam(spu.getCid3(), null, searching);
        //找到param的value，并存储
        for (SpecParamDTO param : params) {
            param.setValue(specValues.get(param.getId()));
        }
        return params;
    }
}
